let requestUrl = null;

function addHandler() {
	requestUrl = "/gang/insert";
	
	$("#id").textbox("readonly", false);
	
	$("#gang_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#gang_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/gang/updateById";

		$("#id").textbox("readonly", true);
		$('#id').textbox("setValue", rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#purpose").textbox("setValue", rowData.purpose);
		$("#builder").textbox("setValue", rowData.builder);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#serverId").textbox("setValue", rowData.serverId);
		$("#builderId").textbox("setValue", rowData.builderId);

		$("#gang_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");

	}
}

function deleteHandler(gangId) {
	$.messager.confirm("提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxGet("/gang/deleteById", {
				"id": gangId
			}, function(resp) {
				showMsg(resp.message);

				$("#gang_list").datagrid("reload");
			}, error);
		}
	});
}

$(document).ready(function() {

	$("#id").textbox({
		width: 120,
		required: true
	});
	
	$("#name").textbox({
		width: 120,
		required: true
	});
	
	$("#grade").numberspinner({
        min: 1,
        max: 5,
        width: 120,
        editable: true,
        required: true
    });

	$("#serverId").combobox({
		url: "/server/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		required: true
	});
	
	$("#builder").textbox({
		width: 120,
		required: true
	});
	
	$("#builderId").textbox({
		width: 120,
		required: true
	});
	
    $("#purpose").textbox({
		width: 320,
    	height: 100,
    	required: true,
		multiline: true
	});
    
	$("#gang_dialog").dialog({
		title: "帮派信息",
		width: 440,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#gang_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#gang_dialog").dialog("close");
						$("#gang_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#gang_dialog").dialog("close");
				$("#gang_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr * 15;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	/**
	 * 可选页码
	 * @type {number[]}
	 */
	let pageList = [15, 30, 100, 200, 500];

	$("#gang_list").datagrid({
		url: "/gang/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		pageList: pageList,
		pageSize: pageList[0],
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "帮派ID", align:"center", width: 50},
			{field: "name", title: "帮派名称", align:"center", width: 50},
			{field: "grade", title: "帮派等级", align:"center", width: 50, sortable: true},
			{field: "purpose", title: "帮派宗旨", align:"center", width: 120
				, formatter: function(value) {
					return "<div class='ell' title='" + value + "'>" + value + "</div>";
				}
			},
			{field: "builder", title: "帮派创始人", align:"center", width: 50},
			{field: "builderId", title: "创始人ID", align:"center", width: 50},
			{field: "serverId", title: "服务器", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/server/selectById", {
							id: value
						}, function (resp) {
							let server = resp.data;

							ajaxGet("/area/selectById", {
								id: server.areaId
							}, function (resp) {
								let area = resp.data;

								$("#server_" + rowIndex).html(area.name + "-" + server.name);
							}, error);
						}, error);

						return "<div id='server_" + rowIndex + "'></div>";
					}

					return "/";
				}
			}, deleteColumn
		]]
	});

});